<template>
    <div class="app-container">
        <div style="display: flex">
            <el-form :model="dates" ref="queryForm" size="small" :inline="true" v-show="showSearch">
              <el-form-item label="查询方式">
                <el-select v-model="secvalue" placeholder="请选择查询方式">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
  
              <el-form-item v-if="secvalue == '按月查询'" label="按月查询">
                <el-date-picker v-model="dates.month" type="month" value-format="yyyy-MM" placeholder="选择月">
                </el-date-picker>
              </el-form-item>
              <el-form-item v-if="secvalue == '按月查询'">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
            <div>
              <el-form :model="dates" ref="queryForm" size="small" :inline="true" v-show="showSearch">
                <el-form-item v-if="secvalue == '按年查询'" label="按年查询">
                  <el-date-picker v-model="dates.years" type="year" value-format="yyyy" placeholder="选择年">
                  </el-date-picker>
                </el-form-item>
                <el-form-item v-if="secvalue == '按年查询'">
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuerys">搜索</el-button>
                  <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>
          <el-button @click="dangyue" type="primary">当月</el-button>
          <el-button @click="dangnian" style="margin-bottom: 10px" type="primary">当年</el-button>
          <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column fixed label="客服名称" prop="serviceName" />
            <el-table-column label="成交金额" prop="totalTransactionPrice" :show-overflow-tooltip="true" />
            <el-table-column label="应收" prop="totalBillReceivable" :show-overflow-tooltip="true" />
            <el-table-column label="实收" prop="totalOfficialReceipts" />
  
            <el-table-column label="上班天数" prop="teacher" width="workDays" />
            <el-table-column label="成单量" prop="orderCount" />
  
            <el-table-column label="平均客单价" prop="averageRate" width="150" />
  
            <el-table-column label="平均天应收" width="150" prop="averageBillReceivable" />
  
            <el-table-column label="平均天成单量" width="150" prop="averageOrderCount" />
  
            <el-table-column label="低档占比" prop="low" >

              <template slot-scope="scope">
            {{ scope.row.low?((scope.row.low)*100).toFixed(2):0}}%
          </template>

            </el-table-column>
  
            <el-table-column label="中档占比" prop="medium" >

              <template slot-scope="scope">
            {{ scope.row.medium?((scope.row.medium)*100).toFixed(2):0}}%
          </template>

            </el-table-column>
            <el-table-column label="高档占比" prop="hight" >
              <template slot-scope="scope">
            {{ scope.row.hight?((scope.row.hight)*100).toFixed(2):0}}%
          </template>

            </el-table-column>
  
            <el-table-column label="退单数量" prop="refundOrderCount" />
            <el-table-column label="退单金额" prop="totalRefundMoney" />
            <el-table-column label="处理客诉量" width="150" prop="customerComplaintCount" />
            <el-table-column width="150" label="平均天退单量" prop="averageRefundCount" />
            <el-table-column width="150" label="平均天退单金额" prop="averageRefundMoney" />
            <el-table-column label="平均天客诉处理量" width="150" prop="averageCustomerComplaintCount" />
            <el-table-column label="总成交率" width="150" prop="averageCustomerComplaintCount" >
  
              <template slot-scope="scope">
            {{ scope.row.followCount?((scope.row.turnover /scope.row.followCount)*100).toFixed(2):0}}%
          </template>
            </el-table-column>
  
            <el-table-column label="新客户成交率" width="150" prop="averageCustomerComplaintCount" >
  
  <template slot-scope="scope">
    
  {{scope.row.newFollowCount? ((scope.row.newTurnover /scope.row.newFollowCount)*100).toFixed(2):0}}%
  </template>
  </el-table-column>
  
  <el-table-column label="老客户成交率" width="150" prop="averageCustomerComplaintCount" >
  
  <template slot-scope="scope">
  {{ scope.row.oldFollowCount?((scope.row.oldTurnover /scope.row.oldFollowCount)*100).toFixed(2):0}}%
  </template>
  </el-table-column>
  <el-table-column label="	成单量" width="150" prop="turnover" />
  <el-table-column label="	新客户成单量" width="150" prop="newTurnover" />
  <el-table-column label="	老客户成单量" width="150" prop="oldTurnover" />
  <el-table-column label="	询单量" width="150" prop="followCount" />
  <el-table-column label="	新客户询单量" width="150" prop="newFollowCount" />
  <el-table-column label="	老客户询单量" width="150" prop="oldFollowCount" />
  <el-table-column label="	新客户及时成单率" width="150" prop="inTimeTurnoverRateNew" />
  <el-table-column label="	老客户及时成单率" width="150" prop="inTimeTurnoverRateOld" />
  <el-table-column label="	新客户利润率" width="150" prop="newCustomersProfitMargin" />
  <el-table-column label="	老客户利润率
" width="150" prop="oldCustomersProfitMargin" />
  
          </el-table>
    </div>
  </template>
  
  <script>
  import { getRole } from "@/api/system/role";
  import tab from './tab.vue'
  import { chulikesu, getjyue, getjyues, zhenggetjyues } from "@/api/new";
  import { treeselect as menuTreeselect } from "@/api/system/menu";
  
  export default {
    name: "Role",
    dicts: ["sys_normal_disable"],
  
    components: { tab },
    data() {
      return {
        // 遮罩层
        loading: true,
        activeName: "first",
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 角色表格数据
        roleList: [],
        zhengroleList: [],
        wanjiekopng: false,
        // 弹出层标题
        title: "",
        kesuwanfrom: {
          orderId: "",
          disposeResult: "",
        },
        kesuwanfromre: {
          disposeResult: [
            { required: true, message: "处理结果不能为空", trigger: "blur" },
          ],
        },
        datess: {
          startTime:'',endTime:''
        },
        // 是否显示弹出层
        open: false,
        // 是否显示弹出层（数据权限）
        openDataScope: false,
        menuExpand: false,
        menuNodeAll: false,
        deptExpand: true,
        deptNodeAll: false,
        // 日期范围
        dateRange: [],
        // 数据范围选项
        secvalue: "",
        // 菜单列表
        menuOptions: [],
        // 部门列表
        deptOptions: [],
        options: [
          {
            value: "按月查询",
            label: "按月查询",
          },
          {
            value: "按年查询",
            label: "按年查询",
          },
        ],
        // 查询参数
        queryParams: {
          startTime: '',
          endTime: '',
          pageNo: 1,
          pageSize: 10,
        },
        dates: {
          month: "",
          years: "",
        },
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "label",
        },
        // 表单校验
        rules: {
          roleName: [
            { required: true, message: "角色名称不能为空", trigger: "blur" },
          ],
          roleKey: [
            { required: true, message: "权限字符不能为空", trigger: "blur" },
          ],
          roleSort: [
            { required: true, message: "角色顺序不能为空", trigger: "blur" },
          ],
        },
      };
    },
    created() {
      this.getList();
    },
    methods: {
      dangyue() {
        // 获取当前日期
        const now = new Date();
  
        // 获取年份，getFullYear()方法会返回一个四位的年份
        const year = now.getFullYear();
  
        // 获取月份，getMonth()方法返回的月份是从0开始的，所以需要加1
        const month = (now.getMonth() + 1).toString().padStart(2, "0");
  
        // 使用模板字符串或者字符串连接来组合年份和月份
        // return `${year}-${month}`;
        this.dates.month = `${year}-${month}`;
        this.getList();
      },
      dangnian() {
        const now = new Date();
  
        // 获取年份，getFullYear()方法会返回一个四位的年份
        const year = now.getFullYear();
  
        console.log(year);
        this.dates.years = `${year}`;
        this.getLists();
      },
      guanbishoukan() {
        this.kesuwanfrom = {
          orderId: "",
          disposeResult: "",
        };
      },
      wanjjie() {
        this.$refs["wanjie"].validate((valid) => {
          if (valid) {
            chulikesu(this.kesuwanfrom).then(() => {
              this.wanjiekopng = false;
              this.getList();
              this.$modal.msgSuccess("处理成功");
            });
          }
        });
      },
  
      /** 查询角色列表 */
      getList() {
        this.loading = true;
        getjyue({ date: this.dates.month }).then((response) => {
          this.roleList = response.data;
          this.loading = false;
        });
      },
      getLists() {
        this.loading = true;
        getjyues({ date: this.dates.years }).then((response) => {
          this.roleList = response.data;
          this.loading = false;
        });
      },
  
      // 所有菜单节点数据
      getMenuAllCheckedKeys() {
        // 目前被选中的菜单节点
        let checkedKeys = this.$refs.menu.getCheckedKeys();
        // 半选中的菜单节点
        let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
        checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
        return checkedKeys;
      },
      // 所有部门节点数据
      getDeptAllCheckedKeys() {
        // 目前被选中的部门节点
        let checkedKeys = this.$refs.dept.getCheckedKeys();
        // 半选中的部门节点
        let halfCheckedKeys = this.$refs.dept.getHalfCheckedKeys();
        checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
        return checkedKeys;
      },
      zhenghandleQuery() {
        this.zhenggetList();
      },
      zhenggetList() {
        this.loading = true;
        zhenggetjyues(this.datess).then((response) => {
          this.zhengroleList = [response.data];
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNo = 1;
        this.getList();
      },
      handleQuerys() {
        this.queryParams.pageNo = 1;
        this.getLists();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dates = {
          month: "",
          years: "",
        };
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.orderId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      // 更多操作触发
      handleCommand(command, row) {
        switch (command) {
          case "handleDataScope":
            this.handleDataScope(row);
            break;
          case "handleAuthUser":
            this.handleAuthUser(row);
            break;
          default:
            break;
        }
      },
      // 树权限（展开/折叠）
      handleCheckedTreeExpand(value, type) {
        if (type == "menu") {
          let treeList = this.menuOptions;
          for (let i = 0; i < treeList.length; i++) {
            this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
          }
        } else if (type == "dept") {
          let treeList = this.deptOptions;
          for (let i = 0; i < treeList.length; i++) {
            this.$refs.dept.store.nodesMap[treeList[i].id].expanded = value;
          }
        }
      },
      // 树权限（全选/全不选）
      handleCheckedTreeNodeAll(value, type) {
        if (type == "menu") {
          this.$refs.menu.setCheckhandleAddedNodes(value ? this.menuOptions : []);
        } else if (type == "dept") {
          this.$refs.dept.setCheckedNodes(value ? this.deptOptions : []);
        }
      },
      // 树权限（父子联动）
      handleCheckedTreeConnect(value, type) {
        if (type == "menu") {
          this.form.menuCheckStrictly = value ? true : false;
        } else if (type == "dept") {
          this.form.deptCheckStrictly = value ? true : false;
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.getMenuTreeselect();
        this.open = true;
        this.title = "添加角色";
      },
  
      /** 修改按钮操作 */
      handleUpdate(row) {
        console.log(this.ids);
        this.kesuwanfrom.orderId = row.orderId || this.ids[0];
        this.wanjiekopng = true;
        this.title = "处理";
      },
      /** 选择角色权限范围触发 */
      dataScopeSelectChange(value) {
        if (value !== "2") {
          this.$refs.dept.setCheckedKeys([]);
        }
      },
      /** 分配数据权限操作 */
      handleDataScope(row) {
        this.reset();
        const deptTreeSelect = this.getDeptTree(row.complaintId);
        getRole(row.complaintId).then((response) => {
          this.form = response.data;
          this.openDataScope = true;
          this.$nextTick(() => {
            deptTreeSelect.then((res) => {
              this.$refs.dept.setCheckedKeys(res.checkedKeys);
            });
          });
          this.title = "分配数据权限";
        });
      },
      /** 分配用户操作 */
      handleAuthUser: function (row) {
        const complaintId = row.complaintId;
        this.$router.push("/system/role-auth/user/" + complaintId);
      },
  
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          "system/role/export",
          {
            ...this.queryParams,
          },
          `role_${new Date().getTime()}.xlsx`
        );
      },
    },
  };
  </script>
  